var e = require;
var t = module;
var o = exports;
Object.defineProperty(o, "__esModule", { value: !0 });
var n = e("UIConfig"),
    i = e("UIBase"),
    r = e("UIDefine"),
    a = { OpenUI_UIName: "OpenUI_UIName", CloseUI_UIName: "CloseUI_UIName" },
    c = new ((function () {
        function e() {
            this.panelBundleName = "Res";
            this.ModEvent = a;
            this.uiStack = [];
            this.uiCache = {};
            this.loadingNum = 0;
            this.delayRemoveDependArr = [];
            this._bundle = {};
        }
        Object.defineProperty(e.prototype, "uiRoot", {
            get: function () {
                if (!cc.isValid(this._uiRoot) || !this._uiRoot) {
                    var e = cc.view.getVisibleSize();
                    this._uiRoot = new cc.Node("GroupView");
                    this._uiRoot.parent = cc.Canvas.instance.node;
                    this._uiRoot.width = e.width;
                    this._uiRoot.height = e.height;
                }
                return this._uiRoot;
            },
            set: function (e) {
                this._uiRoot = e;
            },
            enumerable: !1,
            configurable: !0
        });
        e.prototype.initUiRoot = function (e) {
            this.uiRoot = e;
        };
        e.prototype.initBgNode = function (e) {
            this.bgNode = e;
        };
        e.prototype.getUiid = function (e) {
            return (isNaN(e) && n.UIID[e]) || -1;
        };
        e.prototype.open = function (e, t, o) {
            if (BannerAd.Instance.cityIswhite || e == n.UIID.UISetting) BannerAd.Instance.ShowCustomAd();
            var i = this;
            void 0 === o && (o = !1);
            var r = n.uiConfig[e];
            if (r) {
                if (r.multiple || !this.hasUIInUIStack(e)) {
                    var a = {
                        uiId: e,
                        uiView: null,
                        isClose: !1,
                        zIndex: r.zIndex || 0,
                        uiConfigInfo: r,
                        hideCount: 0,
                        dependCount: 1
                    };
                    r.skipStack || this.addUIStack(a);
                    this.createUI(e, t, function (e) {
                        e && !a.isClose ? i.onUIOpen(a, e, t) : i.destroyValidUI(e, a);
                    });
                } else this.resortUIStack(e, t, o);
            } else cc.warn("uiConfig 未配置", e);
        };
        e.prototype.getRes = function (e, t, o) {
            var n;
            "/" != t[0] && (t = "/" + t);
            return null === (n = cc.assetManager.getBundle(e)) || void 0 === n ? void 0 : n.get(t, o);
        };
        e.prototype.openLoading = function () {
            var e = this;
            this.loadingNum++;
            1 === this.loadingNum &&
                this.open(n.UIID.UILoading, {
                    openCb: function (t) {
                        0 === e.loadingNum && t.closeUI();
                    }
                });
        };
        e.prototype.closeLoading = function () {
            this.loadingNum--;
            if (this.loadingNum <= 0) {
                this.loadingNum = 0;
                this.closeUiByUIID(n.UIID.UILoading);
            }
        };
        e.prototype.getPrefabPathByUIId = function (e) {
            var t = n.uiConfig[e];
            return t ? (t.path && t.path.length > 0 ? t.path + t.prefab : r.BaseUiPath + t.prefab) : null;
        };
        e.prototype.clearUICache = function () {
            for (var e in this.uiCache)
                if (this.uiCache.hasOwnProperty(e)) {
                    var t = this.uiCache[e];
                    this.destroyValidUI(t);
                }
            this.uiCache = {};
        };
        e.prototype.closeAll = function () {
            for (var e = 0, t = this.uiStack; e < t.length; e++) {
                var o = t[e],
                    n = o.uiView;
                o.isClose = !0;
                o.uiView = null;
                this.destroyValidUI(n);
            }
            this.uiStack = [];
            this.loadingNum = 0;
        };
        e.prototype.close = function (e) {
            var t,
                o = this;
            if (!cc.isValid(e)) return !1;
            e.name;
            this.uiStack.length;
            for (var i = 0; i < this.uiStack.length; i++)
                try {
                    var r = this.uiStack[i];
                    !r || !r.uiView || r.uiView.name;
                    if (r.uiView == e) {
                        t = r;
                        this.uiStack.splice(i, 1);
                        break;
                    }
                } catch (e) { }
            var c = e.uiId,
                u = e;
            if (t) {
                var s = t.uiId,
                    p = t.uiView;
                this.removeDependCount(t);
                t.uiConfigInfo.inCloseEndRecover || this.reverseClose(t);
                t.isClose = !0;
                if (!p) return !1;
                this.updateView();
                p._closeEffect(function () {
                    if (t.uiConfigInfo.isCache) {
                        o.uiCache[s] = p;
                        p.node.removeFromParent(!1);
                    } else o.destroyValidUI(p);
                    t.uiView = null;
                    t.uiConfigInfo.inCloseEndRecover && o.reverseClose(t);
                    o.judgeRemoveDepend();
                });
            } else {
                var d = n.uiConfig[e.uiId];
                if (!d.skipStack) {
                    cc.isValid(e) && e.node.destroy();
                    return !1;
                }
                u._closeEffect(function () {
                    if (d.isCache) {
                        o.uiCache[c] = u;
                        u.node.removeFromParent(!1);
                    } else o.destroyValidUI(u);
                });
            }
            EventCenter.emit(a.CloseUI_UIName, n.UIID[c]);
            u.ui_data && u.ui_data.uiCloseCb && u.ui_data.uiCloseCb(u, t);
            return !0;
        };
        e.prototype.onDestroyVerifyStack = function (e) {
            for (var t = 0; t < this.uiStack.length; t++) {
                var o = this.uiStack[t];
                if (o.uiView == e) {
                    if (!o.isClose) {
                        this.removeDependCount(o);
                        this.reverseClose(o);
                        o.isClose = !0;
                        this.judgeRemoveDepend();
                    }
                    this.uiStack.splice(t, 1);
                    break;
                }
            }
        };
        e.prototype.closeUiByUIID = function (e, t) {
            for (var o = 0; o < this.uiStack.length; o++) {
                var n = this.uiStack[o],
                    i = !1;
                if (n.uiId === e) {
                    var r = n.uiView;
                    if (cc.isValid(r)) i = this.close(r);
                    else {
                        n.isClose = !0;
                        this.uiStack.splice(o, 1);
                        i = !0;
                    }
                    if (!t) break;
                    i && o--;
                }
            }
        };
        e.prototype.getUi = function (e) {
            for (var t = 0; t < this.uiStack.length; t++) {
                var o = this.uiStack[t];
                if (o.uiId === e) return o.uiView;
            }
            return null;
        };
        e.prototype.destroyValidUI = function (e, t) {
            if (t && !t.isClose) {
                this.removeDependCount(t);
                this.reverseClose(t);
                t.isClose = !0;
                var o = this.uiStack.indexOf(t);
                o >= 0 && this.uiStack.splice(o, 1);
                this.judgeRemoveDepend();
            }
            e && cc.isValid(e.node) && e.node.destroy();
        };
        e.prototype.hideUIInStack = function (e, t, o, n) {
            void 0 === o && (o = !1);
            void 0 === n && (n = 1e3);
            if (0 == n) {
                cc.error("hideUIInStack 循环超1000次 检查死循环", t);
                return !1;
            }
            if (null == t)
                for (var i = 0, r = this.uiStack; i < r.length; i++) {
                    var a = r[i];
                    a.uiView === e && (t = a);
                }
            if (!t || !cc.isValid(t.uiView)) return !1;
            o && t.hideCount++;
            if (t.isHide || t.isClose) return !1;
            if (o && t.dependCount > t.hideCount) return !1;
            t.isHide = !0;
            t.uiView.hideUI(!0);
            var c = t.uiConfigInfo.dependUIArr;
            if (c.length > 0)
                for (var u = 0, s = c; u < s.length; u++) {
                    var p = s[u],
                        d = this.getUIinUIStack(p);
                    d && this.hideUIInStack(d.uiView, d, !0, --n);
                }
            return !0;
        };
        e.prototype.redisplayUIInStack = function (e, t, o, n) {
            void 0 === o && (o = !1);
            void 0 === n && (n = 1e3);
            if (0 == n) {
                cc.error("redisplayUIInStack 循环超1000次 检查死循环", t);
                return !1;
            }
            if (null == t)
                for (var i = 0, r = this.uiStack; i < r.length; i++) {
                    var a = r[i];
                    a.uiView === e && (t = a);
                }
            if (!t || !cc.isValid(t.uiView)) return !1;
            o && t.hideCount > 0 && t.hideCount--;
            if (!t.isHide || t.isClose) return !1;
            t.isHide = !1;
            t.uiView.redisplayUI(!0);
            var c = t.uiConfigInfo.dependUIArr;
            if (c.length > 0)
                for (var u = 0, s = c; u < s.length; u++) {
                    var p = s[u],
                        d = this.getUIinUIStack(p);
                    d && this.redisplayUIInStack(d.uiView, d, !0, --n);
                }
            return !0;
        };
        e.prototype.getUIStack = function () {
            return this.uiStack;
        };
        e.prototype.createUI = function (e, t, o) {
            var n = this.uiCache[e];
            if (n) {
                delete this.uiCache[e];
                o(n);
            } else {
                var r = this.getPrefabPathByUIId(e);
                r &&
                    this.loadRes(this.panelBundleName, r, cc.Prefab, function (e, r) {
                        if (e) o(null);
                        else if ((n = r.data.getComponent(i.default))) {
                            var a = cc.instantiate(r);
                            (n = a.getComponent(i.default))._setUiData(t);
                            o(n);
                        } else o(null);
                    });
            }
        };
        e.prototype.loadRes = function (e, t, o, n) {
            this.checkGetBundle(e, function (e) {
                var i = e.get(t, o);
                i ? n && n(null, i) : e.load(t, o, n);
            });
        };
        e.prototype.checkGetBundle = function (e, t) {
            var o = this;
            this._bundle[e]
                ? t && t(this._bundle[e])
                : cc.assetManager.loadBundle(e, function (n, i) {
                    if (n) cc.log(e + " uiBundle", n);
                    else {
                        o._bundle[e] = i;
                        t && t(o._bundle[e]);
                    }
                });
        };
        e.prototype.onUIOpen = function (e, t, o) {
            var i = this;
            if (t) {
                e.uiView = t;
                e.uiView.node.active = !0;
                t.uiId = e.uiId;
                t.node.zIndex = e.zIndex;
                (this.uiRoot || cc.director.getScene()).addChild(t.node);
                this.showDependArr(e);
                e.uiConfigInfo.inShowEndHide || this.reverseShow(e);
                t.createdInit();
                t.showUI(o, function () {
                    e.uiConfigInfo.inShowEndHide && i.reverseShow(e);
                    i.updateView();
                });
                EventCenter.emit(a.OpenUI_UIName, n.UIID[e.uiId]);
                o && o.uiOpenCb && o.uiOpenCb(t, e);
            }
        };
        e.prototype.showDependArr = function (e) {
            var t = e.uiConfigInfo.dependUIArr;
            if (t.length > 0)
                for (var o = 0, n = t; o < n.length; o++) {
                    var i = n[o];
                    this.open(i, null, !0);
                }
        };
        e.prototype.removeDependCount = function (e) {
            if (!e.isClose) {
                var t = e.uiConfigInfo.dependUIArr;
                if (t.length > 0)
                    for (var o = 0, n = t; o < n.length; o++) {
                        var i = n[o],
                            r = this.getUIinUIStack(i);
                        if (r) {
                            r.dependCount--;
                            r.dependCount <= 0 && this.pushDelayRemoveDepend(i);
                        }
                    }
            }
        };
        e.prototype.pushDelayRemoveDepend = function (e) {
            this.delayRemoveDependArr.push(e);
        };
        e.prototype.judgeRemoveDepend = function () {
            for (var e = 0; e < this.delayRemoveDependArr.length; e++) {
                var t = this.delayRemoveDependArr[e],
                    o = this.getUIinUIStack(t);
                o && !o.isClose && o.dependCount <= 0 && this.closeUiByUIID(t);
            }
        };
        e.prototype.reverseShow = function (e) {
            var t = this.uiStack.indexOf(e);
            if (t > 0) {
                var o = e.uiConfigInfo.uiAdditionType;
                if (void 0 !== e.uiConfigInfo.uiAdditionType)
                    for (var n = t - 1; n >= 0; n--) {
                        var i = this.uiStack[n],
                            a = i.uiConfigInfo.uiFrom;
                        if (!i.isClose && void 0 !== a)
                            if (
                                o == r.EmUIAddition.destroyAllScene ||
                                (o == r.EmUIAddition.hideAllScene && a == r.EmUIFrom.Popup) ||
                                ((o == r.EmUIAddition.destroyAllPopup || o == r.EmUIAddition.destroyPrePopup) &&
                                    a == r.EmUIFrom.Popup)
                            ) {
                                cc.isValid(i.uiView) && i.uiView.closeUI();
                                if (o == r.EmUIAddition.destroyPrePopup) break;
                            } else if (
                                (o == r.EmUIAddition.hideAllScene ||
                                    ((o == r.EmUIAddition.hideAllPopup || o == r.EmUIAddition.hidePrePopup) &&
                                        a == r.EmUIFrom.Popup)) &&
                                !i.uiConfigInfo.isIgnoreHide
                            ) {
                                this.hideUIInStack(i.uiView, i);
                                if (o == r.EmUIAddition.hidePrePopup) break;
                            }
                    }
            }
        };
        e.prototype.reverseClose = function (e) {
            var t = this.uiStack.indexOf(e);
            -1 == t && (t = this.uiStack.length);
            var o = e.uiConfigInfo.uiAdditionType;
            if (void 0 !== e.uiConfigInfo.uiAdditionType)
                for (var n = t - 1; n >= 0; n--) {
                    var i = this.uiStack[n],
                        a = i.uiConfigInfo.uiFrom;
                    if (
                        !i.isClose &&
                        void 0 !== a &&
                        ((o == r.EmUIAddition.hideAllScene && a == r.EmUIFrom.Scene) ||
                            ((o == r.EmUIAddition.hideAllPopup || o == r.EmUIAddition.hidePrePopup) &&
                                a == r.EmUIFrom.Popup))
                    ) {
                        this.redisplayUIInStack(i.uiView, i);
                        if (o == r.EmUIAddition.hidePrePopup || o == r.EmUIAddition.hideAllScene) break;
                    }
                }
        };
        e.prototype.updateView = function () {
            if (0 !== this.uiStack.length) {
                for (var e, t = this.uiStack.length - 1; t >= 0; t--) {
                    var o = this.uiStack[t],
                        n = o.uiView;
                    if (
                        cc.isValid(n) &&
                        !o.isHide &&
                        void 0 !== o.uiConfigInfo.showPublicBg &&
                        (e = o.uiConfigInfo.showPublicBg)
                    )
                        break;
                }
                this.bgNode && void 0 !== e && (this.bgNode.active = e);
            } else this.bgNode && (this.bgNode.active = !0);
        };
        e.prototype.addUIStack = function (e) {
            this.uiStack.push(e);
        };
        e.prototype.getUIinUIStack = function (e) {
            for (var t = 0, o = this.uiStack; t < o.length; t++) {
                var n = o[t];
                if (n.uiId === e) return n;
            }
            return null;
        };
        e.prototype.removeElement = function (e, t) {
            var o = t.indexOf(e);
            o >= 0 && t.splice(o, 1);
        };
        e.prototype.hasUIInUIStack = function (e) {
            var t = this.getUIinUIStack(e);
            !t || (null != t.uiView && null != t.uiView.node) || this.removeElement(t, this.uiStack);
            return t && null != t.uiView && null != t.uiView.node;
        };
        e.prototype.resortUIStack = function (e, t, o) {
            void 0 === o && (o = !1);
            for (var i, r = !1, c = -1, u = 0; u < this.uiStack.length; u++) {
                var s = this.uiStack[u];
                if (i) {
                    r = !0;
                    this.uiStack[u - 1] = s;
                    this.uiStack[u] = i;
                } else if (s.uiId === e) {
                    i = s;
                    c = u;
                }
            }
            if (i) {
                if (o) {
                    i.dependCount++;
                    i.isHide && i.hideCount < i.dependCount && this.redisplayUIInStack(i.uiView, i, !1);
                } else if (i.isHide) {
                    this.redisplayUIInStack(i.uiView, i, !1);
                    this.reverseShow(i);
                }
                r && this.updateView();
                var p = i.uiView;
                if (p && p.node) {
                    p.node.setSiblingIndex(-1);
                    p.onOpen(t);
                    o || EventCenter.emit(a.OpenUI_UIName, n.UIID[i.uiId]);
                } else this.uiStack.splice(c, 1);
            }
        };
        return e;
    })())();
o.default = c;
